package dice.sinanya.dice.game;

import dice.sinanya.dice.imal.Check;
import dice.sinanya.entity.EntityTypeMessages;
import dice.sinanya.exceptions.CantInPrivateException;
import dice.sinanya.exceptions.NotEnableInGroupException;
import dice.sinanya.exceptions.OnlyManagerException;

import java.util.ArrayList;

import static dice.sinanya.system.MessagesBanList.groupSwitchHashMap;
import static dice.sinanya.system.MessagesOb.obList;
import static dice.sinanya.tools.checkdata.CheckCantInPrivate.checkCantInPrivate;
import static dice.sinanya.tools.getinfo.GetMessagesProperties.entityGame;
import static dice.sinanya.tools.makedata.Sender.sender;

/**
 * @author SitaNya
 * @date 2019/9/11
 * @email sitanya@qq.com
 * @qqGroup 162279609
 * 有任何问题欢迎咨询
 * <p>
 * 类说明:
 */
public class Ob implements Check {

    private final EntityTypeMessages entityTypeMessages;

    public Ob(EntityTypeMessages entityTypeMessages) {
        this.entityTypeMessages = entityTypeMessages;
    }

    public void input() throws NotEnableInGroupException, CantInPrivateException {
        checkEnable();
        checkCantInPrivate(entityTypeMessages);
        if (obList.containsKey(entityTypeMessages.getFromGroupString())) {
            ArrayList<String> obListInGroup = obList.get(entityTypeMessages.getFromGroupString());
            if (obListInGroup.contains(entityTypeMessages.getFromQqString())) {
                sender(entityTypeMessages, entityGame.getObAlreadyInsert(entityTypeMessages.getFromGroupString()));
            } else {
                obListInGroup.add(entityTypeMessages.getFromQqString());
                obList.put(entityTypeMessages.getFromGroupString(), obListInGroup);
                sender(entityTypeMessages, entityGame.getObInsert(entityTypeMessages.getFromGroupString()));
            }
        } else {
            ArrayList<String> obListInGroup = new ArrayList<>();
            obListInGroup.add(entityTypeMessages.getFromQqString());
            obList.put(entityTypeMessages.getFromGroupString(), obListInGroup);
            sender(entityTypeMessages, entityGame.getObInsert(entityTypeMessages.getFromGroupString()));
        }
    }

    public void exit() throws NotEnableInGroupException, CantInPrivateException {
        checkEnable();
        checkCantInPrivate(entityTypeMessages);
        if (obList.containsKey(entityTypeMessages.getFromGroupString())) {
            ArrayList<String> obListInGroup = obList.get(entityTypeMessages.getFromGroupString());
            if (obListInGroup.contains(entityTypeMessages.getFromQqString())) {
                obListInGroup.remove(entityTypeMessages.getFromQqString());
                if (obListInGroup.isEmpty()) {
                    obList.remove(entityTypeMessages.getFromGroupString());
                } else {
                    obList.put(entityTypeMessages.getFromGroupString(), obListInGroup);
                }
                sender(entityTypeMessages, entityGame.getObRemove(entityTypeMessages.getFromGroupString()));
            } else {
                sender(entityTypeMessages, entityGame.getObAlreadyRemove(entityTypeMessages.getFromGroupString()));
            }
        } else {
            sender(entityTypeMessages, entityGame.getObAlreadyRemove(entityTypeMessages.getFromGroupString()));
        }
    }

    public void clr() throws OnlyManagerException, NotEnableInGroupException, CantInPrivateException {
        checkAudit(entityTypeMessages);
        checkEnable();
        checkCantInPrivate(entityTypeMessages);
        obList.remove(entityTypeMessages.getFromGroupString());
        sender(entityTypeMessages, entityGame.getObClear(entityTypeMessages.getFromGroupString()));
    }

    public void list() throws OnlyManagerException, NotEnableInGroupException, CantInPrivateException {
        checkAudit(entityTypeMessages);
        checkEnable();
        checkCantInPrivate(entityTypeMessages);
        if (obList.containsKey(entityTypeMessages.getFromGroupString())) {
            ArrayList<String> obListInGroup = obList.get(entityTypeMessages.getFromGroupString());
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(entityGame.getObShow(entityTypeMessages.getFromGroupString()));
            for (String qqId : obListInGroup) {
                stringBuilder.append("\n");
                stringBuilder.append(entityTypeMessages.getMsgSender().GETTER.getGroupMemberInfo(entityTypeMessages.getFromGroupString(), qqId).getCard());
                stringBuilder.append("（").append(qqId).append(")");
            }
            sender(entityTypeMessages, stringBuilder.toString());
        } else {
            sender(entityTypeMessages, entityGame.getObEmpty(entityTypeMessages.getFromGroupString()));
        }
    }


    private void checkEnable() throws NotEnableInGroupException {
        if (groupSwitchHashMap.containsKey(entityTypeMessages.getFromGroup()) && !groupSwitchHashMap.get(entityTypeMessages.getFromGroup()).isOb()) {
            throw new NotEnableInGroupException(entityTypeMessages);
        }
    }
}
